package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
@Entity
@Table(name="staff")
public class Staff extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	
	public String name; //姓名
	
	public String gender; //性别
	
	public String title; //职位
	
	public Integer seniority; //工龄
	
	public String job; //岗位
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date on_board_date; //入职日期
	
	public String record; //工作记录
	
	public Integer authority; //权限
	
	public String image; //相片
	public static Model.Finder<Integer, Staff> find = new Model.Finder<Integer, Staff>(Integer.class, Staff.class);

	public static Page<Staff> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Staff> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setName(String d) {
		name= d;
	}
	public String getName() {
		return name;
	}
	public void setGender(String d) {
		gender= d;
	}
	public String getGender() {
		return gender;
	}
	public void setTitle(String d) {
		title= d;
	}
	public String getTitle() {
		return title;
	}
	public void setSeniority(Integer d) {
		seniority= d;
	}
	public Integer getSeniority() {
		return seniority;
	}
	public void setJob(String d) {
		job= d;
	}
	public String getJob() {
		return job;
	}
	public void setOn_board_date(Date d) {
		on_board_date= d;
	}
	public Date getOn_board_date() {
		return on_board_date;
	}
	public void setRecord(String d) {
		record= d;
	}
	public String getRecord() {
		return record;
	}
	public void setAuthority(Integer d) {
		authority= d;
	}
	public Integer getAuthority() {
		return authority;
	}
	public void setImage(String d) {
		image= d;
	}
	public String getImage() {
		return image;
	}
	public static Staff findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Staff v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
	public static Staff findByName(String d) {
		return find.where().eq("name", d).findUnique();
	}
	public static String getNameJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Staff v: find.all()) {
			node.put(v.getName().toString(), v.getName().toString()); 
		}
		return node.toString();
	}
	public static Staff findByGender(String d) {
		return find.where().eq("gender", d).findUnique();
	}
	public static String getGenderJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Staff v: find.all()) {
			node.put(v.getGender().toString(), v.getGender().toString()); 
		}
		return node.toString();
	}
	public static Staff findByTitle(String d) {
		return find.where().eq("title", d).findUnique();
	}
	public static String getTitleJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Staff v: find.all()) {
			node.put(v.getTitle().toString(), v.getTitle().toString()); 
		}
		return node.toString();
	}
	public static Staff findBySeniority(Integer d) {
		return find.where().eq("seniority", d).findUnique();
	}
	public static String getSeniorityJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Staff v: find.all()) {
			node.put(v.getSeniority().toString(), v.getSeniority().toString()); 
		}
		return node.toString();
	}
	public static Staff findByJob(String d) {
		return find.where().eq("job", d).findUnique();
	}
	public static String getJobJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Staff v: find.all()) {
			node.put(v.getJob().toString(), v.getJob().toString()); 
		}
		return node.toString();
	}
	public static Staff findByOn_board_date(Date d) {
		return find.where().eq("on_board_date", d).findUnique();
	}
	public static String getOn_board_dateJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Staff v: find.all()) {
			node.put(v.getOn_board_date().toString(), v.getOn_board_date().toString()); 
		}
		return node.toString();
	}
	public static Staff findByRecord(String d) {
		return find.where().eq("record", d).findUnique();
	}
	public static String getRecordJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Staff v: find.all()) {
			node.put(v.getRecord().toString(), v.getRecord().toString()); 
		}
		return node.toString();
	}
	public static Staff findByAuthority(Integer d) {
		return find.where().eq("authority", d).findUnique();
	}
	public static String getAuthorityJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Staff v: find.all()) {
			node.put(v.getAuthority().toString(), v.getAuthority().toString()); 
		}
		return node.toString();
	}
}
